//
//  HWMyWalletHeadView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/20.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWMyWalletHeadView.h"
#import "HWSegmentOneView.h"
#import "HWExchangeViewController.h"
#import "HWExchangeBookCoinViewController.h"
#import "HWCashDrawViewController.h"

@interface HWMyWalletHeadView()
@property (nonatomic, strong)UILabel *currentMoneyLabel;//当前金额
@property (nonatomic, strong)UILabel *currentCoinLabel;//当前金币
@property (nonatomic, strong)UILabel * accumulatedLabel;//累计收益
@property (nonatomic, strong)UILabel * coinAccumlatedLabel;//金币收益

@property (nonatomic, strong)HWSegmentOneView * segmentOneView;//金币获得记录下次级标签栏

@property (nonatomic, strong)NSMutableArray * lineViewArr;//
@property (nonatomic, strong)NSMutableArray * BtnArr;//
@property (nonatomic, strong)NSMutableArray * segmentArr;//

@property (nonatomic, strong)NSString * currentMoney;//余额
@property (nonatomic, strong)NSString * currentCoin;//金币

@end

@implementation HWMyWalletHeadView

- (NSMutableArray *)lineViewArr
{
    if (!_lineViewArr)
    {
        _lineViewArr = [[NSMutableArray alloc]init];
    }
    return _lineViewArr;
}

- (NSMutableArray *)BtnArr
{
    if (!_BtnArr)
    {
        _BtnArr = [[NSMutableArray alloc]init];
    }
    return _BtnArr;
}

- (NSMutableArray *)segmentArr
{
    if (!_segmentArr)
    {
        _segmentArr = [[NSMutableArray alloc]init];
    }
    return _segmentArr;
}

- (UILabel *)currentMoneyLabel
{
    if (!_currentMoneyLabel)
    {
        _currentMoneyLabel = [HWUtilties creatLabelWithFrame:CGRectMake(29*kUIScaleBasePlus,54*kUIScaleBasePlus,0,30*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentLeft
                                                    textClor:LCHexColor(0xFFFFFF)
                                                     textFont:44*kUIScaleBasePlus];
        _currentMoneyLabel.numberOfLines=0;
    }
    return _currentMoneyLabel;
}

- (UILabel *)currentCoinLabel
{
    if (!_currentCoinLabel)
    {
        _currentCoinLabel = [HWUtilties creatLabelWithFrame:CGRectMake(165*kUIScaleBasePlus,72*kUIScaleBasePlus,0,30*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0xFFE8A3)
                                                   textFont:19*kUIScaleBasePlus];
        _currentCoinLabel.numberOfLines = 0;
    }
    return _currentCoinLabel;
}

- (UILabel *)accumulatedLabel
{
    if (!_accumulatedLabel)
    {
        _accumulatedLabel = [HWUtilties creatLabelWithFrame:CGRectMake(240*kUIScaleBasePlus,72*kUIScaleBasePlus,0,30*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentCenter
                                                   textClor:LCHexColor(0xFFE8A3)
                                                   textFont:19*kUIScaleBasePlus];
        _accumulatedLabel.numberOfLines = 0;
    }
    return _accumulatedLabel;
}

- (UILabel *)coinAccumlatedLabel
{
    if (!_coinAccumlatedLabel)
    {
        _coinAccumlatedLabel = [HWUtilties creatLabelWithFrame:CGRectMake(28*kUIScaleBasePlus,120*kUIScaleBasePlus,250*kUIScaleBasePlus,10*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0xFFE8A3)
                                                   textFont:10*kUIScaleBasePlus];
    }
    return _coinAccumlatedLabel;
}

- (HWSegmentOneView *)segmentOneView
{
    if (!_segmentOneView)
    {
        _segmentOneView = [[HWSegmentOneView alloc]initWithFrame:CGRectMake(0,260*kUIScaleBasePlus,SCREEN_WIDTH,35*kUIScaleBasePlus)];
    }
    return _segmentOneView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self creatUI];
        [self segment];
        [self addSubview:self.segmentOneView];
    }
    return self;
}

#pragma mark - creatUI
- (void)creatUI
{
    UIImageView *footImageView = [[UIImageView alloc]initWithFrame:CGRectMake(7*kUIScaleBasePlus,8*kUIScaleBasePlus,SCREEN_WIDTH-14*kUIScaleBasePlus,212*kUIScaleBasePlus)];
    footImageView.image = [UIImage imageNamed:@"mywallet_bg"];
    [self addSubview:footImageView];
    
    UILabel *currentLabel = [HWUtilties creatLabelWithFrame:CGRectMake(28*kUIScaleBasePlus,30*kUIScaleBasePlus,80*kUIScaleBasePlus,10*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0xFFE8A3)
                                                   textFont:10*kUIScaleBasePlus];
    currentLabel.text = @"当前余额(元)";
    [self addSubview:currentLabel];
    
    UIButton *widthMoneyButton = [HWUtilties creatButtonWithFrame:CGRectMake(166*kUIScaleBasePlus,30*kUIScaleBasePlus,50*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                                     textAligment:NSTextAlignmentCenter
                                                             text:@"兑换余额"
                                                         textClor:LCHexColor(0xFE7067)
                                                        buttonBGC:LCHexColor(0xFFF3D4)
                                                         textFont:10*kUIScaleBasePlus
                                                           target:self
                                                           method:@selector(withDraw)];
    widthMoneyButton.layer.masksToBounds = YES;
    widthMoneyButton.layer.cornerRadius = 4;
    [self addSubview:widthMoneyButton];
    
    UILabel *currentMoneylabel = [HWUtilties creatLabelWithFrame:CGRectMake(166*kUIScaleBasePlus,CGRectGetMaxY(widthMoneyButton.frame)+6*kUIScaleBasePlus,50*kUIScaleBasePlus,10*kUIScaleBasePlus)
                                                    textAligment:NSTextAlignmentLeft
                                                        textClor:LCHexColor(0xFFE8A3)
                                                        textFont:10*kUIScaleBasePlus];
    currentMoneylabel.text = @"当前金币";
    [self addSubview:currentMoneylabel];
    
    UILabel *accumulatedMoneylabel = [HWUtilties creatLabelWithFrame:CGRectMake(240*kUIScaleBasePlus,CGRectGetMaxY(widthMoneyButton.frame)+6*kUIScaleBasePlus,80*kUIScaleBasePlus,10*kUIScaleBasePlus)
                                                    textAligment:NSTextAlignmentLeft
                                                        textClor:LCHexColor(0xFFE8A3)
                                                        textFont:10*kUIScaleBasePlus];
    accumulatedMoneylabel.text = @"累计收益(元)";
    [self addSubview:accumulatedMoneylabel];
    
    
    [self addSubview:self.currentMoneyLabel];
    
    [self addSubview:self.currentCoinLabel];
    
    [self addSubview:self.accumulatedLabel];
    
    [self addSubview:self.coinAccumlatedLabel];
    
    UIImageView *logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(28*kUIScaleBasePlus,137*kUIScaleBasePlus,10*kUIScaleBasePlus,10*kUIScaleBasePlus)];
    logoImageView.image = [UIImage imageNamed:@"notice_wallet"];
    [self addSubview:logoImageView];
    
    UILabel *contextLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(logoImageView.frame)+5*kUIScaleBasePlus,137*kUIScaleBasePlus,230*kUIScaleBasePlus,10*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0xFFE8A3)
                                                   textFont:10*kUIScaleBasePlus];
    contextLabel.text = @"金币转换汇率受每日营收收益影响(上下略有浮动)";
    [self addSubview:contextLabel];
    
    UIButton *exchangeBtn = [HWUtilties creatButtonWithFrame:CGRectMake(7*kUIScaleBasePlus,157*kUIScaleBasePlus,180*kUIScaleBasePlus,60*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentCenter
                                                        text:@"兑换书币"
                                                    textClor:LCHexColor(0xFFF3D4)
                                                   buttonBGC:[UIColor clearColor]
                                                    textFont:15*kUIScaleBasePlus
                                                      target:self
                                                      method:@selector(exchangeCoin)];
    [exchangeBtn setImage:[UIImage imageNamed:@"exchange_currency"] forState:UIControlStateNormal];
    [exchangeBtn setImageEdgeInsets:UIEdgeInsetsMake(0,-10,0,0)];
    [self addSubview:exchangeBtn];
    
    UIButton *getMoneyBtn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(exchangeBtn.frame),157*kUIScaleBasePlus,180*kUIScaleBasePlus,60*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentCenter
                                                        text:@"余额提现"
                                                    textClor:LCHexColor(0xFFF3D4)
                                                   buttonBGC:[UIColor clearColor]
                                                    textFont:15*kUIScaleBasePlus
                                                      target:self
                                                      method:@selector(getMoney)];
    [getMoneyBtn setImage:[UIImage imageNamed:@"exchange_currency"] forState:UIControlStateNormal];
    [getMoneyBtn setImageEdgeInsets:UIEdgeInsetsMake(0,-10,0,0)];
    [self addSubview:getMoneyBtn];
    
    UIImageView *hotImageView = [[UIImageView alloc]initWithFrame:CGRectMake(143*kUIScaleBasePlus,160*kUIScaleBasePlus,28*kUIScaleBasePlus,28*kUIScaleBasePlus)];
    hotImageView.image = [UIImage imageNamed:@"hots"];
    [self addSubview:hotImageView];

}

#pragma mark - segment//标题栏
- (void)segment
{
    
    NSArray *titleArr = @[@"金币获得记录",@"金币兑换记录",@"余额提现记录"];
    CGFloat width = SCREEN_WIDTH/3;
    CGFloat height = 37*kUIScaleBasePlus;
    CGFloat lineWidth = 30*kUIScaleBasePlus;
    CGFloat lineHeight = 2*kUIScaleBasePlus;
    CGFloat margin = 95*kUIScaleBasePlus;

    for (int i=0; i<3; i++)
    {
        UIButton *btn = [HWUtilties creatButtonWithFrame:CGRectMake(width*i,219*kUIScaleBasePlus,width,height)
                                            textAligment:NSTextAlignmentCenter
                                                    text:titleArr[i]
                                                textClor:LCHexColor(0x878787)
                                               buttonBGC:[UIColor clearColor]
                                                textFont:15*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(segmentClick:)];
        btn.tag = i;
        
        if (i == 0)
        {
            [btn setTitleColor:LCHexColor(0xF54337) forState:UIControlStateNormal];
            
        }
        [self.BtnArr addObject:btn];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(48*kUIScaleBasePlus+(lineWidth+margin)*i,257*kUIScaleBasePlus,lineWidth,lineHeight)];
        lineView.backgroundColor = LCHexColor(0xF54337);
        if (i != 0)
        {
            lineView.hidden = YES;
        }
        [self.lineViewArr addObject:lineView];
        
        [self addSubview:btn];
        [self addSubview:lineView];
    }
    
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0,258*kUIScaleBasePlus,SCREEN_WIDTH,1*kUIScaleBasePlus)];
    bottomLineView.backgroundColor = LCHexColor(0xEBE5E1);
    [self addSubview:bottomLineView];
}

#pragma mark - 获取数据
- (void)getDate:(NSArray *)arr
{
    
    NSDictionary *dic = arr[0];
    
    self.currentMoneyLabel.text = dic[@"currentMoney"];
    self.currentMoney = dic[@"currentMoney"];
    [self.currentMoneyLabel sizeToFit];
    
    self.currentCoinLabel.text = dic[@"currentCoin"];
    self.currentCoin = dic[@"currentCoin"];
    [self.currentCoinLabel sizeToFit];
    
    self.accumulatedLabel.text = dic[@"accumulated"];
    [self.accumulatedLabel sizeToFit];

    self.coinAccumlatedLabel.text = dic[@"coinAccumlated"];
    
    [self.segmentOneView getSegmentTitle:arr[1]];
}

#pragma mark -Method
- (void)segmentClick:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self setHeight:295*kUIScaleBasePlus];
        self.segmentOneView.hidden = NO;
    }
    else
    {
        [self setHeight:260*kUIScaleBasePlus];
        self.segmentOneView.hidden = YES;
    }
    
    if (self.getBackSegmentChangeBlock)
    {
        self.getBackSegmentChangeBlock([NSString stringWithFormat:@"%ld",sender.tag]);
    }
    
    [self.BtnArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = obj;
        if (idx != sender.tag)
        {
            [btn setTitleColor:LCHexColor(0x878787) forState:UIControlStateNormal];
            UIView *lineView = self.lineViewArr[idx];
            lineView.hidden = YES;
        }
        else
        {
            [btn setTitleColor:LCHexColor(0xF54337) forState:UIControlStateNormal];
            UIView *lineView = self.lineViewArr[sender.tag];
            lineView.hidden = NO;
        }
    }];
}

//余额提现
- (void)getMoney
{
    if ([current_User.isHideinfo.stringValue isEqualToString:@"0"])
    {
        HWCashDrawViewController *vc = [[HWCashDrawViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
    }
    else
    {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showInfoWithStatus:@"暂未开放"];
        DISMISS_SVP(1.0f);
    }
}

//兑换书币
- (void)exchangeCoin
{
    HWExchangeBookCoinViewController *vc = [[HWExchangeBookCoinViewController alloc]init];
    vc.bookCoin = self.currentCoin;
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}

//兑换余额
- (void)withDraw
{
    HWExchangeViewController *vc = [[HWExchangeViewController alloc]init];
    vc.bookCoin = self.currentCoin;
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}
@end
